﻿<template>
  <a-drawer
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="简称" prop="Abbreviation">
          <a-input v-model="entity.Abbreviation" autocomplete="off"> </a-input>
        </a-form-model-item>

        <a-form-model-item label="上级物品分类" prop="ParentId">
          <a-tree-select
            :style="{ width: '100%' }"
            :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
            :treeData="ParentIdTreeData"
            placeholder="请选择上级物品分类"
            v-model="entity.ParentId"
          ></a-tree-select>
          <!-- treeDefaultExpandAll -->
        </a-form-model-item>

        <a-form-model-item label="物品分类名称" prop="Name">
          <a-input v-model="entity.Name" autocomplete="off" @blur="inputblur"> </a-input>
        </a-form-model-item>
        <a-form-model-item label="物品分类编码" prop="Code">
          <a-input v-model="entity.Code" autocomplete="off"> </a-input>
        </a-form-model-item>
        <a-form-model-item label="是否可见" prop="VisibleOrNot" v-if="entity.ParentId">
          <a-radio-group @change="IsEnable" v-model="entity.VisibleOrNot" :default-value="true" button-style="solid">
            <a-radio-button :value="true"> 是 </a-radio-button>
            <a-radio-button :value="false"> 否 </a-radio-button>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Remarks">
          <a-textarea v-model="entity.Remarks" autocomplete="off"></a-textarea>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1
      }"
    >
      <a-button
        :style="{ marginRight: '8px' }"
        @click="
          () => {
            this.visible = false
          }
        "
        >取消</a-button
      >
      <a-button type="primary" @click="handleSubmit">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },

  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {
        Code: [{ required: true, message: '请输物品分类编号', trigger: 'blur' }],
        Name: [{ required: true, message: '请输物品分类名称', trigger: 'blur' }]
      },
      title: '',
      ParentIdTreeData: []
    }
  },
  methods: {
    async inputblur(name) {
      await this.$http
        .post('/PB/PB_MaterialType/GetmaterialTypePinYin?MaterialTypeName=' + this.entity.Name, {})
        .then(resJson => {
          if (resJson.Success) {
            this.$set(this.entity, 'Code', resJson.Data)
          }
        })
    },
    IsEnable(e) {
      this.disableds = e.target.value
      this.disableds ? (this.entity.Ratio = 0) : (this.entity.Ratio = '')
    },
    init() {
      this.$http.post('/PB/PB_MaterialType/GetTreeDataList').then(resJson => {
        if (resJson.Success) {
          this.ParentIdTreeData = resJson.Data
        }
      })
      this.visible = true
      this.entity = {
        VisibleOrNot: true
      }
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/PB/PB_MaterialType/GetTheData', { id: id }).then(async resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }

        this.loading = true
        var entity = { ...this.entity }
        // entity.Code = this.entity.Code2 + this.entity.Code
        this.$http.post('/PB/PB_MaterialType/SaveData', entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false
            this.parentObj.data = []
            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
